package cn.kgc.controller.paycontroller;

import cn.kgc.dtos.Dto;

import cn.kgc.service.ItripHotelOrderPayService;
import org.apache.commons.io.IOUtils;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Administrator on 2018/12/3.
 */
@RestController
@RequestMapping("/api")
public class WxController {

    @Resource
    private ItripHotelOrderPayService itripHotelOrderPayService;

    /**
     * 微信生产二维码
     * @param orderNo
     * @return
     */
    @RequestMapping( value = "/wxpay/createqccode/{orderNo}",method = RequestMethod.GET)
    public Dto createqccode(@PathVariable("orderNo") String orderNo){
        return  itripHotelOrderPayService.updateOrderByorderNo(orderNo);
    }

    /**
     * 微信回调函数
     * @param request
     * @param response
     */
    @RequestMapping("/jieguo")
    public  void jieguo(HttpServletRequest request,HttpServletResponse response){
        InputStream is= null;
        try {
            is = request.getInputStream();
            String xmlResult= IOUtils.toString(is,"utf-8");
            boolean bu= itripHotelOrderPayService.huidiao(xmlResult);
            if (bu){
                String msgToWx="<xml> \n" +
                        "\n" +
                        "  <return_code><![CDATA[SUCCESS]]></return_code>\n" +
                        "  <return_msg><![CDATA[OK]]></return_msg>\n" +
                        "</xml> \n";
                response.getWriter().write(msgToWx);
                response.getWriter().flush();
                System.out.println("回调函数成功");
            }
        } catch (IOException e) {
            System.out.println(e);
        }

    }

    /**
     * 页面回调响应
     * @param orderNo
     * @return
     */
    @RequestMapping(value = "/wxpay/queryorderstatus/{orderNo}",method = RequestMethod.GET)
    public  Dto queryorderstatus(@PathVariable("orderNo") String orderNo){
        return itripHotelOrderPayService.RenderResponse(orderNo);
    }
}
